import * as vscode from 'vscode';
import { TreeItem, TreeItemCollapsibleState, TreeDataProvider, Uri, window } from 'vscode';
import { join } from 'path';
import * as fs from 'fs';
import { KeyObject } from 'crypto';

let ITEM_ICON_MAP:Map<string,any>;

export function loadItems() {
    if(ITEM_ICON_MAP) {return ITEM_ICON_MAP;}
    
    let rootpath = vscode.workspace.rootPath;
    let data:any = "" , map = new Map<string,any>();
    if( rootpath ){
        if( fs.existsSync(rootpath + "\\.lproject.json" ) ){
            data = fs.readFileSync(rootpath + "\\.lproject.json","utf-8");
            data = JSON.parse( data );
            for(let key in data){
                map.set( key , data[key] || {} );
            }
        }else{
            vscode.window.showWarningMessage('找不到配置文件 .lproject.json');
        }
    }else{
		vscode.window.showWarningMessage('您还没有打开项目(WorkSpace is null)');
    }
    ITEM_ICON_MAP = map;
    return map;
}

export class TreeItemNode extends TreeItem {
    //记录节点下的links
    public children: TreeItemNode[] | undefined;
    public links: Object;
    constructor(
        public readonly label: string,
        public readonly collapsibleState: TreeItemCollapsibleState,
        public readonly options : Object
    ){
        super(label, collapsibleState);
        this.options = options;
        this.links = {};
    }

    command = {
        title: this.label,
        command: 'itemClick',
        tooltip: this.label,
        arguments: [
            this,
            this.label
        ]
    };

    iconPath = TreeItemNode.getIconUriForLabel(this.label);

    static getIconUriForLabel(label: string):Uri {
        loadItems();
        let icon = ITEM_ICON_MAP.get(label).icon ? ITEM_ICON_MAP.get(label).icon : 'default.svg';
        return Uri.file(join(__filename,'..', '..' ,'src' ,'assert', icon +''));
    }
}


export class TreeViewProvider implements TreeDataProvider<TreeItemNode>{
    onDidChangeTreeData?: import("vscode").Event<TreeItemNode | null | undefined> | undefined;
    
    public data: TreeItemNode[] | undefined;
    // constructor() {
    //     this.data = [
    //         new TreeItemNode('line1', [new TreeItemNode('line1-sub1'), new TreeItemNode('line1-sub2')]),
    //         new TreeItemNode('line2', [new TreeItemNode('line2-sub1'), new TreeItemNode('line2-sub2')]),
    //         new TreeItemNode('line3', [new TreeItemNode('line3-sub1'), new TreeItemNode('line3-sub2')])
    //     ];
    // }

    getTreeItem(element: TreeItemNode): TreeItem | Thenable<TreeItem> {
        return element;
    }

    getChildren(element?: TreeItemNode | undefined): import("vscode").ProviderResult<TreeItemNode[]> {
        loadItems();
        
        if (element === undefined) {
            return this.data;
        }
        return element.children;

        let arr:any[] = [];
        ITEM_ICON_MAP.forEach((value:any , key:string) =>{
            arr.push({
                key : key,
                opt : value
            });
        });

        const rearr = arr.map(item => new TreeItemNode(
            item.key as string, //lable
            TreeItemCollapsibleState.None as TreeItemCollapsibleState ,
            item.opt));
        return rearr;
    }

    public static initTreeViewItem(){
        const treeViewProvider = new TreeViewProvider();
        window.registerTreeDataProvider('treeView-item',treeViewProvider);
    }
}